// pages/userAddress/userAddress.js
const addressService = require('../../service/addressService.js')
const utils = require('../../utils/util.js');
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: '3UJBZ-JK6W5-HHLIQ-QOF7B-CQ4U7-D3FJZ' // 必填
});
// load promise 
require('../../utils/wx-pro.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userAddressList: [],
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddressList()
  },

  getUserAddressList: function () {
    addressService.getUserAddressList().then(data => {
      if (data.code == 200) {
        console.log(data.response.xcxAddressList)
        this.setData({
          userAddressList: data.response.xcxAddressList
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch(() => {
        console.log('error')
      })
  },

  deleAddress(event){
    console.log(event.currentTarget.dataset.id)
    var addressId = event.currentTarget.dataset.id
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定删除该地址？',
      success: function (res) {
        if (res.confirm) {
          var params = {
            addressId: addressId
          }
          addressService.deleteXcxAddress(params).then(data => {
            if (data.code == 200) {
              wx.showToast({
                title: '已删除',
                icon: 'succes',
                duration: 1000
              })
              _this.getUserAddressList()
            } else {
              this.showToastFuc(data.message)
            }
          })
            .catch(() => {
              console.log('error')
            })
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },

  addressLink() {
    var that = this
    wx.chooseAddress({
      success: function (res) {
        console.log(res)
        that.map(res)
      }
    })
  },

  map: function (data) {
    var that = this;
    demo.geocoder({
      address: data.provinceName + data.cityName + data.countyName + data.detailInfo,
      success: function (res) {
        var params = {
          provinceId: data.nationalCode.substr(0, 2) + '0000',
          cityId: data.nationalCode.substr(0, 4) + '00',
          districtId: data.nationalCode,
          detailAddress: data.provinceName + data.cityName + data.countyName + data.detailInfo,
          longitude: res.result.location.lng,
          latitude: res.result.location.lat,
          contactName: data.userName,
          contactPhone: data.telNumber
        }
        addressService.saveXcxAddress(params).then(data => {
          if (data.code == 200) {
            console.log('已保存')
            that.getUserAddressList()
          } else {
            this.showToastFuc(data.message)
          }
        })
          .catch(() => {
            console.log('error')
          })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },

  editAddress(event) {
    console.log(event.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../editAddress/editAddress?id=' + event.currentTarget.dataset.id
    })
    console.log('../editAddress/editAddress?id=' + event.currentTarget.dataset.id)

  }
})